package com.app.twth.ui.putaway.submit;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityPutawaySubmitBinding;
import com.app.twth.ui.putaway.PutatayInfo;
import com.app.twth.ui.putaway.PutawayDetails;
import com.app.twth.ui.tally.lpn.TalliedSku;
import com.app.twth.ui.tally.lpn.TallyLPNAdapter;
import com.app.twth.utils.FastJsonUtil;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class PutawaySubmitActivity extends BaseActivity<ActivityPutawaySubmitBinding, PutawaySubmitViewModel> {
    private String TAG = getClass().getSimpleName();

    private String lpn;
    private String suggestLocation;
    private TallyLPNAdapter adapter;
    private List<TalliedSku> tallyList = new ArrayList<>();


    @Override
    protected void initListener() {
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.tallyDetailsLiveData.observe(this, data -> {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    @Override
    protected String setTitle() {
        return "Putaway(Submit)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setNext(getString(R.string.submit));
        Bundle bundle = getIntent().getBundleExtra(AppRouter.K_EXTRA);
        PutatayInfo putatayInfo= (PutatayInfo) bundle.getSerializable("data");
        if (putatayInfo==null) {
            finish();
            return;
        }
        tallyList = putatayInfo.getSkuInfoList();
        if (tallyList == null || tallyList.size() == 0) {
            finish();
            return;
        }
        lpn = putatayInfo.getIbLpn();
        suggestLocation = putatayInfo.getLocationCode();
        binding.tvLpn.setText(lpn);
        binding.tvLocationCode.setText("pending " + lpn + " in ：");
        binding.tvCode.requestFocus();

        if (Utils.isEmpty(suggestLocation)) {
            binding.tvSuggestLocation.setVisibility(View.GONE);
            binding.tvSuggestLocationHint.setVisibility(View.GONE);
        } else {
            binding.tvSuggestLocation.setText(suggestLocation);
        }
        adapter = new TallyLPNAdapter(this, tallyList, null);
        binding.recyclerview.setHasFixedSize(true);
        binding.recyclerview.setItemAnimator(new DefaultItemAnimator());

        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerview.setLayoutManager(layoutManager1);
        //添加装饰类
//        binding.recyclerview.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
        binding.recyclerview.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_reset:
                binding.tvCode.setText("");
                break;
            case R.id.iv_scan:
                setScan();
                break;
            case R.id.tv_next:
                if (System.currentTimeMillis() - timeMillis < spaceTime) {//两秒内触发一次
                    return;
                }
                timeMillis = System.currentTimeMillis();
                String location = binding.tvCode.getText().toString();
                if (Utils.isEmpty(location)) {
                    ToastUtil.showToast("Please enter location.");
                    return;
                }
                PutataySubmitQr putataySubmitQr = new PutataySubmitQr();
                putataySubmitQr.setIbLpn(lpn);
                putataySubmitQr.setLocationCode(location);
                viewModel.putawaySubmit(putataySubmitQr);
                break;
        }
    }

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCode.setText(barcodeStr);
    }

}
